package com.ithema.controller;

import com.itheima.domain.Book;

import com.ithema.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
*
*      此类 没采取 协议方式进行
*                                  这里讲解的是springmvc下的拦截器作用
*                 拦截器在controller表现层中用的！！！
*
* */


@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private BookService bookService;

//    查全部
    @GetMapping
    public List<Book> Books_All() {
        List<Book> books = bookService.selectBookAll();
        System.out.println(books);
        return books;
    }

//    查单个
    @GetMapping("/{id}")
    public Book Books_ById(@PathVariable("id") int bookId) {
        Book book = bookService.selectBook_Id(bookId);
        System.out.println(book);
        return book;
    }

//    保存
    @PostMapping
    public boolean Book_save(@RequestBody Book book) {
        boolean b = bookService.saveBook(book);
        return b;
    }

//    根据ID删除
    @DeleteMapping("/{id}")
    public boolean Book_delete(@PathVariable("id") Integer id){
        boolean b = bookService.deleteBook(id);
        return b;
    }

//    修改值
    @PutMapping()
    public boolean Book_update(@RequestBody Book book){
        boolean update = bookService.update(book);
        return update;
    }

}
